package com.lcm.jnode.kit;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

import com.google.common.collect.Lists;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StringKit;
import com.lcm.jnode.utils.ConfigUtil;
import com.lcm.jnode.utils.DESUtils;

/**
 * 对字符串的简单处理
 * @author L.cm
 * @date 2013-6-6 下午5:08:06
 */
public class StringsKit {

	private static final Logger log = Logger.getLogger(StringsKit.class);
	private static final String[] strArr = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
	private static final String SECRET = ConfigUtil.get("secret");
    /**
     * 截取中文字符串
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String sub(String string, int length) {
    	if(StringKit.notBlank(string) && string.length() > length){
    		try {
    			if (string.getBytes("GBK").length > length){
	    			int size = 0;
		    		for (int i = 0; i < string.length(); i++) {
						String temp = string.substring(i, i + 1);
						byte[] bytes = temp.getBytes("GBK");
						if(bytes.length == 1){
							++size;
						}else{
							size += 2;
						}
						if(size >= length){
							string = string.substring(0, i + 1) + "...";
							break;
						}
					}
    			}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				log.error(e);
			}
    		
    	}
    	return string;
    }

    /**
     * 获取url里的path
     * @param @param queryString
     * @param @return    设定文件
     * @return List<String>    返回类型
     * @throws
     */
    public static List<String> comboList(String queryString){
        if(queryString.indexOf("&") > 0){
            queryString = queryString.substring(0, queryString.indexOf("&"));
        }
        List<String> fileList = Lists.newArrayList();
        if(queryString.indexOf(",") > 0){
            String[] paths = queryString.split(",");
            for (String path : paths) {
                if(StringKit.notBlank(path)){
                    fileList.add(PathKit.getWebRootPath() + path);
                }
            }
        }else{
            fileList.add(PathKit.getWebRootPath() + queryString);
        }
        return fileList;
    }
    
    /**
     * 生成一个10位的tonken用于http cache
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String getTonken(){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            sb.append(strArr[new Random().nextInt(9)]);
        }
        return sb.toString();
    }
    
    /**
     * 加密密码
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String pwdEncrypt(String pwd){
        return DigestUtils.md5Hex(DigestUtils.md5Hex(pwd + SECRET));
    }
    
    /**
     * 加密cookie
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String cookieEncrypt(String email, String pwd){
        return new DESUtils(SECRET).encryptString(email + ":" + pwdEncrypt(pwd));
    }
    
    /**
     * 解密cookie
     * @param @param cookieString
     * @param @return    设定文件
     * @return String[]    返回类型
     * @throws
     */
    public static String[] cookieDecryption(String cookieString){
        cookieString = new DESUtils(SECRET).decryptString(cookieString);
        if(cookieString.indexOf(":") != -1) {
            return cookieString.split(":");
        }else{
            return null;
        }
    }
    
    /**
     * 百度获ip获取到的城市处理 供大众点评
     * 暂用，以后会用类是分词的技术，好吧完全没有算法的概念
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String cityMatcher(String city) {
    	String regex = "(.+)[市|省|自治区]";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(city);
		if (matcher.matches()) {
			return matcher.group(1);
		}else {
			return null;
		}
    }
    
    /**
     * 字符串全角转半角
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String togglecase(String string) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            sb.append(CharKit.regularize(string.charAt(i)));
        }
        return sb.toString();
    }
}
